package com.obas.business.analogy.compnent{
	
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.business.component.tree.treeclass.ErrorNode;
   import com.obas.business.component.tree.treeclass.LoadingNode;
   import com.obas.business.component.tree.treeclass.Node;
   import com.obas.common.constant.NetworkLibConstant;
   import com.obas.common.constant.StringConstant;
   import com.obas.common.event.CommonOperationEvent;
   import com.obas.common.util.ResourceUtil;
   import com.obas.model.mature.NetworkBusiness;
   import com.obas.model.mature.NetworkServing;
//   
//   import com.pktech.itest.view.menu.CaseMenu;
//   import com.pktech.itest.view.menu.FunctionMenu;
//   import com.pktech.itest.view.menu.ProjectEmptyMenu;
//   import com.pktech.itest.view.menu.ProjectMenu;
//   import com.pktech.itest.view.menu.ScenarioMenu;
//   import com.pktech.widgets.controls.Menu;

   import flash.events.Event;

   import mx.collections.ArrayCollection;
   import mx.core.Container;
   import mx.events.MenuEvent;
   import com.obas.business.component.tree.LoadingTree;

   [Event(name="{com.obas.common.event.CommonOperationEvent.REQUEST_ID}", type="com.obas.common.event.CommonOperationEvent")]
   public class ExplorerTree extends LoadingTree {
      public function ExplorerTree() {

      }

      override protected function buildMenu(container:Container):void {
//         var projectMenu:Menu = new ProjectMenu();
//         container.addChild(projectMenu);
//         _menuMap[NetworkLibConstant.ITEMTYPE_PROJECT] = projectMenu;

//         var scenarioMenu:Menu = new ScenarioMenu();
//         container.addChild(scenarioMenu);
//         _menuMap[NetworkLibConstant.ITEMTYPE_SCENARIO] = scenarioMenu;
//
//         var caseMenu:Menu = new CaseMenu();
//         container.addChild(caseMenu);
//         _menuMap[NetworkLibConstant.ITEMTYPE_CASE] = caseMenu;
//
//         var functionMenu:Menu = new FunctionMenu();
//         container.addChild(functionMenu);
//         _menuMap[NetworkLibConstant.ITEMTYPE_FUNCTION] = functionMenu;
//
//         var projectEmptyMenu:Menu = new ProjectEmptyMenu();
//         container.addChild(projectEmptyMenu);
//         _menuMap[NetworkLibConstant.ITEMTYPE_EMPTY] = projectEmptyMenu;
      }

      //点击展开节点，获取节点下的node
      override protected function callRemoteService(node:Node):void {
         var entity:* = node.entity;
		 
         if (entity is NetworkBusiness) {
            dispatchEvent(new CommonOperationEvent([entity.id, node], CommonOperationEvent.GET_NETWORKBUSINESS));
         }
		 if (entity is Object) {
			 var code:String = entity.code;
			
			 var result:ArrayCollection =null;
			 
			 if("NetworkQuality"==code) {
//				 children:[//
//					 {name:"有线数据业务",id:1, children:[//
//						 {name:"ADSL",id:1},//
//						 {name:"EPON",id:2},//
//						 {name:"LAN",id:3}//
//					 ]},//
			
				 
				result = new ArrayCollection	 			( [//
					 {name:"有线数据业务",code:"有线数据业务",id:1},
					 {name:"有线语音业务",code:"有线语音业务",id:2},//
					 {name:"综合业务",code:"综合业务",id:3},//
					 {name:"无线数据业务", code:"无线数据业务",id:4},//
					 {name:"无线语音业务",code:"无线语音业务",id:5}//
				]);
				
				 
			 } else if("有线数据业务"==code) {
				 
				 result = new ArrayCollection	 			( [//
					 {name:"ADSL",code:"ADSL",id:1,nodetype:"leaf"},//
					 						 {name:"EPON",code:"EPON",id:2,nodetype:"leaf"},//
					 						 {name:"LAN",code:"LAN",id:3,nodetype:"leaf"}//
				 ]);
			 } else if("BusinessQuality"==code) {
				 result = new ArrayCollection	 			( [//
					 {name:"应用",code:"APP",nodetype:"leaf"},//
					 {name:"业务",code:"SVC",nodetype:"leaf"}//
					 
				 ]);
				 				 
			 } else if("UserQuality"==code) {
				 result = new ArrayCollection	 			( [//
					 {name:"用户应用",code:"USERAPP",nodetype:"leaf"},//
					 {name:"用户业务",code:"USERSVC",nodetype:"leaf"}//
					 
				 ]);
			 
			 }
			
			 
			 
			 if(null!=result) {
			 removeNodeStat(node);
			 var childNode:Node = null;
			 for each (var childEntity:Object in result) {
				 childNode = nodeFactory.createNode(childEntity);
				 childNode.parentNode = node;
				 if(childEntity.nodetype) {
					 childNode.nodeType=childEntity.nodetype;
					 childNode.children = null;
				 }				
				 childNode.type = getNodeType(childEntity);
//				 if (childNode.type == NetworkLibConstant.ITEMTYPE_SCRIPT) {
//					 childNode.children = null;
//				 }
				 node.children.addItem(childNode);
			 }
			 
			 node.initialized = true;
			 expandItem(node, false);
			 expandItem(node, true);
			 }
			 
		 }			 
		 
//         else if (entity is SystemScenario) {
//            dispatchEvent(new CommonOperationEvent([entity.id, node], CommonOperationEvent.GET_CASE));
//         }
//         else if (entity is SystemUsecase) {
//            dispatchEvent(new CommonOperationEvent([entity.id, node], CommonOperationEvent.GET_FUNCTION));
//         }
//         else if (entity is SystemFunction) {
//            dispatchEvent(new CommonOperationEvent([entity.id, node], CommonOperationEvent.GET_SCRIPT));
//         }
      }

      [ResponseHandler("{com.obas.common.event.CommonOperationEvent.RETURN_ID}")]
      public function onCommonOperationEvent(event:CommandCompletionEvent):void {
         var loadingTreeEvent:CommonOperationEvent = event.request as CommonOperationEvent;
         var requestNode:Node = loadingTreeEvent.spec[1];

         if (event.error) {
            const errorMsg:String = ResourceUtil.getErrorString(StringConstant.EXPLORER_ERROR, event.error);
            addErrorStat(requestNode, errorMsg);
            return ;
         }

         var result:ArrayCollection = event.result as ArrayCollection;
         removeNodeStat(requestNode);
         var childNode:Node = null;
         for each (var childEntity:Object in result) {
            childNode = nodeFactory.createNode(childEntity);
            childNode.parentNode = requestNode;
            childNode.type = getNodeType(childEntity);
            if (childNode.type == NetworkLibConstant.ITEMTYPE_SCRIPT) {
               childNode.children = null;
            }
            requestNode.children.addItem(childNode);
         }

         requestNode.initialized = true;
         expandItem(requestNode, false);
         expandItem(requestNode, true);
      }

      public function getNodeType(item:*):String {
		 
         if (item is NetworkBusiness) {
            return NetworkLibConstant.ITEMTYPE_NETWORKBUSINESS;
         }
//         else if (item is SystemScenario) {
//            return NetworkLibConstant.ITEMTYPE_SCENARIO;
//         }
//         else if (item is SystemUsecase) {
//            return NetworkLibConstant.ITEMTYPE_CASE;
//         }
//         else if (item is SystemFunction) {
//            return NetworkLibConstant.ITEMTYPE_FUNCTION;
//         }
         else if (item is NetworkServing) {
            return NetworkLibConstant.ITEMTYPE_SCRIPT;
         }
		 if (item is Object) {
			 return NetworkLibConstant.ITEMTYPE_OBJECT;
		 }
		 
         return "";
      }
   }
}